/**
 * Created by mac1 on 2017/11/14.
 */
var net = require('net');
var redis = require('redis');

var server = net.createServer(function (socket) {//为每个连接到聊天服务器上的用户定义设置逻辑
  var subscriber;
  var publisher ;
  socket.on('connect', function () {
    subscriber  = redis.createClient();
    subscriber.subscribe('main_chat_room');
    subscriber.on('message', function (channel, message) {
      socket.write('Channel ' + channel + ': ' + message);
    });
    publisher = redis.createClient();
  });

  socket.on('data', function (data) {
    publisher.publish('main_chat_room', data);
  });

  socket.on('end', function () {
    subscriber.unsubscribe('main_chat_room');

    subscriber.end();
    publisher.end();
  });
});

server.listen(3000);